# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import os.path

import scrapy
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from scrapy.pipelines.files import FilesPipeline

class PptPipeline(FilesPipeline):
    def get_media_requests(self, item, info):
        # 文件下载链接交给调度器入队列
        yield scrapy.Request(url=item['ppt_down_url'],meta={'item':item})
    def file_path(self, request, response=None, info=None, *, item=None):
        # 调整文件保存目录
        item=request.meta['item']
        filename='{}/{}{}'.format(
            item['class_name'],
            item['ppt_name'],
            # 获取文件后缀
            os.path.splitext(item['ppt_down_url'])[1]
        )
        return filename